/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package customvalidator;

import javax.faces.validator.FacesValidator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
/**
 *
 * @author NGUYEN CONG DAT
 */
@FacesValidator("customvalidator.PhoneNumberValidator")
public class PhoneNumberValidator implements Validator {
    @Override
     public void validate(FacesContext facesContext, UIComponent arg1, Object value) throws ValidatorException {
        String inputValue = value + "";
        System.out.println(inputValue);
        int length = inputValue.length();
        String message = "";
        boolean error = false;
        if (length < 8) {
            message = message + "Phone number must have at least 8 digits!";
            error = true;
        }
        try {
            Integer.parseInt(inputValue);
        }
        catch (NumberFormatException e) {
            message = message + "\n Phone number must not contain any non-digit character!";
            error = true;
        }
        if (error == true) {
            FacesMessage msg = new FacesMessage("Phone number validation failed.",message);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }
}
